package jfreechart.tsp.ui;

import org.jfree.chart.*;
import org.jfree.chart.annotations.XYLineAnnotation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;

//综合图 : 散点图+折线图
//网上没找到这种综合图,自己结合网上资料整了一个,凑合用吧
public class ComprehensiveChart extends ApplicationFrame {
    private static final long serialVersionUID = 1L;
    private static JFreeChart chart;

    double maxX = Double.MIN_VALUE;
    double minX = Double.MAX_VALUE;
    double minY = Double.MAX_VALUE;
    double maxY = Double.MIN_VALUE;

    public ComprehensiveChart(String applicationTitle, HashSet<Point> dataSet, List<Segment> segments) {
        super(applicationTitle);
        setContentPane(createDemoPanel(dataSet, segments));
    }

    public static void main(String[] args) {
        HashSet<Point> dataSet = new HashSet<Point>();
        ComprehensiveChart demo = new ComprehensiveChart("AnnealChart_frame", dataSet, null);
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
        demo.save(chart, "AnnealChart.jpeg");
    }

    private XYDataset createDataset(HashSet<Point> dataSet) {
        final XYSeries xySeries = new XYSeries("Point");
        for (Point point : dataSet) {
            xySeries.add(point.x, point.y);
            maxY = maxY < point.y ? point.y : maxY;
            minY = minY > point.y ? point.y : minY;
            maxX = maxX < point.x ? point.x : maxX;
            minX = minX > point.x ? point.x : minX;
        }
        final XYSeriesCollection dataset = new XYSeriesCollection();
        dataset.addSeries(xySeries);
        return dataset;
    }

    private JFreeChart createChart(HashSet<Point> dataSet, List<Segment> segments) {
        //创建主题样式
        StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
        //设置标题字体
        standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 20));
        //设置图例的字体
        standardChartTheme.setRegularFont(new Font("宋书", Font.PLAIN, 15));
        //设置轴向的字体
        standardChartTheme.setLargeFont(new Font("宋书", Font.PLAIN, 15));
        //应用主题样式
        ChartFactory.setChartTheme(standardChartTheme);//设置主题样式,解决中文乱码问题

        JFreeChart chart = ChartFactory.createScatterPlot("散点图",
                "行",
                "列",
                createDataset(dataSet),
                PlotOrientation.VERTICAL, true, false, false);

        //通过XYLineAnnotation画折线
        XYPlot xyplot = (XYPlot) chart.getPlot();//XY坐标系本身
        for (Segment segment : segments) {
            //设置折线颜色和粗细
            xyplot.addAnnotation(new XYLineAnnotation(segment.start.x, segment.start.y, segment.end.x, segment.end.y, new BasicStroke(), Color.BLUE));
            maxY = maxY < segment.start.y ? segment.start.y : maxY;
            minY = minY > segment.start.y ? segment.start.y : minY;
            maxX = maxX < segment.start.x ? segment.start.x : maxX;
            minX = minX > segment.start.x ? segment.start.x : minX;
        }

        //设置X轴属性
        NumberAxis numberaxis = (NumberAxis) xyplot.getDomainAxis();
        numberaxis.setAutoRangeIncludesZero(false);
        numberaxis.setTickMarkInsideLength(2.0F);
        numberaxis.setTickMarkOutsideLength(0.0F);
        numberaxis.setAxisLineStroke(new BasicStroke(0.5f));
        numberaxis.setUpperMargin(0.1);
        numberaxis.setLowerMargin(0.1);
//        numberaxis.setUpperBound(maxX+maxX*0.05);//设置坐标最大值
//        numberaxis.setLowerBound(minX-minX*0.5);
//        numberaxis.setAutoTickUnitSelection(false);
//        numberaxis.setTickUnit(new NumberTickUnit(1)); //坐标间距设置为1

        //设置Y轴属性
        NumberAxis numberaxis1 = (NumberAxis) xyplot.getRangeAxis();
        numberaxis1.setTickMarkInsideLength(2.0F);
        numberaxis1.setTickMarkOutsideLength(0.0F);
        numberaxis1.setUpperMargin(0.1);//设置最高的一个 Item 与Y轴顶端的距离 (相当于总长度的10%)
        numberaxis1.setLowerMargin(0.1);//设置最低的一个 Item 与Y轴底端的距离
//        numberaxis1.setUpperBound(maxY+maxY*0.05);
//        numberaxis1.setLowerBound(minY-minY*0.5);
        numberaxis1.setAxisLineStroke(new BasicStroke(0.5f));
//        numberaxis1.setAutoTickUnitSelection(false);
//        numberaxis1.setTickUnit(new NumberTickUnit(1)); //坐标间距设置为1

        //设置散点属性
        XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot.getRenderer();
        xylineandshaperenderer.setSeriesOutlinePaint(0, Color.WHITE);//散点边框颜色
        xylineandshaperenderer.setUseOutlinePaint(true);//使用散点边框
        xylineandshaperenderer.setSeriesOutlineStroke(0, new BasicStroke(1.0f));//散点边框宽度
        xylineandshaperenderer.setSeriesShape(0, new java.awt.geom.Ellipse2D.Double(-5.5, -5.5, 11.0, 11.0));//设置散点形状和大小(x,y,w,h)
//        xylineandshaperenderer.setSeriesPaint(0, Color.RED);//设置散点颜色
        return chart;
    }

    public JPanel createDemoPanel(HashSet<Point> dataSet, List<Segment> segments) {
        chart = createChart(dataSet, segments);
        ChartPanel chartPanel = new ChartPanel(chart);
        chartPanel.setPreferredSize(new java.awt.Dimension(560, 560));//设置面板大小
        return chartPanel;
    }

    public void save(JFreeChart chart, String fileName) {
        int width = 640; /* Width of the image */
        int height = 480; /* Height of the image */
        File pieChart = new File(fileName);
        try {
            ChartUtilities.saveChartAsJPEG(pieChart, chart, width, height);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
